﻿using ModernShopping.AppCode;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ModernShopping.Account
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnDangKy_Click(object sender, EventArgs e)
        {
            Company company = new Company();
            company.Name = TxtTenCongTy.Text;
            company.Phone = TxtSoDienThoai.Text;
            company.Address = TxtDiaChi.Text;
            company.Email = TxtEmail.Text;
            company.Logo = imgAvatar.ImageUrl;
            string Username = TxtTenDangNhap.Text;
            string Password = TxtMatKhau.Text;
            Countdown.ShowPopup();
            if (DbCompany.CreateNewAccount(Username: Username, Password: Password, Company: company))
            {
                if (FileUpload.HasFile)
                {
                    if (!File.Exists(Server.MapPath("~/images/Logo")))
                    {
                        Directory.CreateDirectory(Server.MapPath("~/images/Logo"));
                    }
                    string URL = "~/images/Logo/{0}{1}";
                    URL = string.Format(URL, company.Code, System.IO.Path.GetExtension(FileUpload.FileName));
                    FileUpload.SaveAs(Server.MapPath(URL));
                    imgAvatar.ImageUrl = URL;
                    DbCompany.UpdateLogo(Username, URL);
                }
                Countdown.InitCountdown(5, "Bạn đã đăng ký thành công", "Hệ thống sẽ tự chuyển về trang đăng nhập trong {0} giây nữa", WebUtil.Page.LOGIN);
                Countdown.Start();
                ModalPopupExtender.Show();
            }
        }
    }
}